package com.example.permissionservice.controller;

import com.example.permissionservice.common.R;
import com.example.permissionservice.service.PermissionService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/permission")
@RequiredArgsConstructor
public class PermissionController {

    private final PermissionService permissionService;

    @PostMapping("/user/{userId}/bind-role/{roleCode}")
    public R<Void> bindRole(@PathVariable("userId") Long userId, @PathVariable("roleCode") String roleCode) {
        permissionService.bindRole(userId, roleCode);
        return R.success();
    }

    @PostMapping("/user/{userId}/bind-default-role")
    public R<Void> bindDefaultRole(@PathVariable("userId") Long userId) {
        permissionService.bindDefaultRole(userId);
        return R.success();
    }

    @GetMapping("/user/{userId}/role-code")
    public R<String> getUserRoleCode(@PathVariable("userId") Long userId) {
        String roleCode = permissionService.getUserRoleCode(userId);
        return R.success(roleCode);
    }

    @PostMapping("/user/{userId}/upgrade-to-admin")
    public R<Void> upgradeToAdmin(@PathVariable("userId") Long userId) {
        permissionService.upgradeToAdmin(userId);
        return R.success();
    }

    @PostMapping("/user/{userId}/downgrade-to-user")
    public R<Void> downgradeToUser(@PathVariable("userId") Long userId) {
        permissionService.downgradeToUser(userId);
        return R.success();
    }
} 